package www.ld.com.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import www.ld.com.dictionary.UseStatusEnum;
import www.ld.com.exception.ValidateException;
import www.ld.com.mapper.RolesMapper;
import www.ld.com.mapper.UserMapper;
import www.ld.com.domain.permission.pojo.ResourceRolesPO;
import www.ld.com.domain.roles.pojo.RolesPO;
import www.ld.com.domain.roles.query.RolesQuery;
import www.ld.com.domain.roles.vo.OutRolesVO;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class RolesService {
    @Resource
    private RolesMapper rolesMapper;
    @Resource
    private UserMapper userMapper;

    public Page<RolesPO> pageRolesByPostQuery(RolesQuery rolesQuery) {
        return rolesMapper.selectPage(
                new Page<RolesPO>(rolesQuery.getCurrent(), rolesQuery.getSize()),
                new LambdaQueryWrapper<RolesPO>()
                        .select(RolesPO::getId, RolesPO::getStatus, RolesPO::getName, RolesPO::getSort)
                        .like(StrUtil.isNotBlank(rolesQuery.getName()), RolesPO::getName, rolesQuery.getName())
                        .eq(Objects.nonNull(rolesQuery.getStatus()), RolesPO::getStatus, rolesQuery.getStatus())
                        .orderByAsc(RolesPO::getSort)
        );
    }

    @Transactional
    public void saveRolesPO(RolesPO rolesPO, List<String> resources) {
        rolesMapper.insert(rolesPO);
        if (Objects.nonNull(resources) && !resources.isEmpty())
            rolesMapper.insertResource(resources, rolesPO.getId());
    }

    @Transactional
    public void updateRolesPOById(RolesPO rolesPO, List<String> resources) {
        if(rolesPO.getStatus()== UseStatusEnum.STOP_USING && rolesMapper.existRoles(String.valueOf( rolesPO.getId())) ){
            throw new ValidateException("修改失败：该角色有用户使用,角色状态不能改为“停用”");
        }
        rolesMapper.updateById(rolesPO);
        rolesMapper.deleteResourceByRolesId(rolesPO.getId());
        if (Objects.nonNull(resources) && !resources.isEmpty())
            rolesMapper.insertResource(resources, rolesPO.getId());
    }

    public OutRolesVO getOutRolesVOById(String id) {
        RolesPO rolesPO = rolesMapper.selectById(id);
        List<ResourceRolesPO> permissionResource = rolesMapper.findResourceByRolesId(Long.valueOf(id));
        OutRolesVO outRolesVO = BeanUtil.toBean(rolesPO, OutRolesVO.class);
        List<String> resources = permissionResource.stream().map(v -> {
            return v.getResourceCode();
        }).collect(Collectors.toList());
        outRolesVO.setResources(resources);
        return outRolesVO;
    }

    @Transactional
    public void deleteRolesById(String id) {
        if (rolesMapper.existRoles(id))
            throw new ValidateException("删除失败：该角色有用户使用");
        rolesMapper.deleteResourceByRolesId(Long.valueOf(id));
        rolesMapper.deleteById(Long.valueOf(id));

    }

    public List<RolesPO> findRolesPO() {
        return rolesMapper.selectList(new LambdaQueryWrapper<RolesPO>()
                .select(RolesPO::getName, RolesPO::getId,RolesPO::getStatus)
        );
    }


}
